package cn.kgc.controller;

import cn.kgc.exception.exception;
import cn.kgc.pojo.*;
import cn.kgc.service.DevService;
import cn.kgc.util.Page;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @描述
 * @author：LY
 * @2018/11/9 11:11
 */
@Controller
public class DevController {

    @Resource
    private DevService devService;

    /**
     * 删除
     */
    @RequestMapping(value = "appinfodelete")
    public String appdelete(@RequestParam("id") String id){
        devService.deleteByPrimaryKey(id);
        return "redirect:appinfolist";
    }

    /**
     * 版本修改
     * @return
     */
    @RequestMapping(value = "appversionmodify")
    public String appversionmodify(@RequestParam("vid")String vid,@RequestParam("aid")String aid,Model model){
        Version versionInfo = devService.versionInfo(aid);
        List<Version> versionInfos = devService.versionInfos(aid);

        model.addAttribute("appVersionList",versionInfos);
        model.addAttribute("appVersion",versionInfo);
        return "developer/appversionmodify";
    }

    /**
     * 版本修改操作
     * @return
     */
    @RequestMapping(value = "appversionmodifysave")
    public String appversionmodify(Version version){
        devService.updateByPrimaryKeySelective(version);
        return "redirect:appinfolist";
    }



    /**
     * 版本添加跳转
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "appversionadd")
    public String appversionadd(Model model,@RequestParam("id") String id,HttpServletRequest request){
        request.getSession().setAttribute("banbenInsertID",id);
        List<Version> listVersion = devService.versionInfos(id);
        model.addAttribute("appVersionList",listVersion);
        model.addAttribute("appId",id);
        return "developer/appversionadd";
    }

    /**
     * 版本添加操作
     * @param version
     * @param fileupload
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "versionOperation",method = RequestMethod.POST)
    public String versionOperation(Version version,
                                   @RequestParam("a_downloadLink") MultipartFile fileupload,
                                   HttpServletRequest request) throws IOException {

        String path = request.getSession().getServletContext().getRealPath("/statics/uploadfiles");

        if(!fileupload.isEmpty()){
            version.setDownloadLink("/statics/uploadfiles"+File.separator+fileupload.getOriginalFilename());
            version.setApkLocPath(path+File.separator+fileupload.getOriginalFilename());
            version.setApkFileName(fileupload.getOriginalFilename());
            fileupload.transferTo(new File(path+File.separator+fileupload.getOriginalFilename()));
        }
        version.setCreationDate(new Date());
        version.setAppId(Long.valueOf(request.getSession().getAttribute("banbenInsertID").toString()) );

        devService.insertSelective(version);
        return "redirect:appinfolist";
    }


    /**
     * 视图查询
     */
    @RequestMapping(value = "appview")
    public String appview(@RequestParam("id") String appinfoid,Model model){
        UserExcesion userExcesion = devService.allQuerByID(appinfoid);
        List<Version> listVersion = devService.versionInfos(appinfoid);

        model.addAttribute("appInfo",userExcesion);
        model.addAttribute("appVersionList",listVersion);
        return "developer/appinfoview";
    }


    /***
     * 登录
     * @param request
     * @return
     */
    @RequestMapping(value = "devLogin",method = RequestMethod.POST)
    public String login(String devCode, String devPassword, HttpServletRequest request){

        dev dev = devService.login(devCode,devPassword);
        if(dev==null){
            request.setAttribute("error",exception.LOGIN_NOUSER);
            return "devlogin";
        }
        request.getSession().setAttribute("devLogin",dev);
        return "developer/main";
    }



    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "appinfomodify")
    public String appinfomodify(@RequestParam("id") String id,Model model){
        UserExcesion userExcesion = devService.allQuerByID(id);
        model.addAttribute("appInfo",userExcesion);
        return "developer/appinfomodify";
    }


    /**
     * 修改操作
     */
    @RequestMapping(value = "appinfomodifysave")
    public String appinfomodifysave(Info info, @RequestParam("attach")MultipartFile fileupload,HttpServletRequest request) throws IOException {

        String path = request.getSession().getServletContext().getRealPath("/statics/uploadfiles");

        if(!fileupload.isEmpty()){
            info.setLogoPicPath(fileupload.getOriginalFilename());
            fileupload.transferTo(new File(path+File.separator+fileupload.getOriginalFilename()));
        }
        devService.updateByPrimaryKeySelective(info);
        return "redirect:appinfolist";
    }










    /**
     * 添加
     */
    @RequestMapping(value = "appinsert")
    public String appinsert(@ModelAttribute("Info") Info info,
                            HttpServletRequest request){
        return "developer/appinfoadd";
    }
    @RequestMapping(value = "appinsertOperation",method = RequestMethod.POST)
    public String appinsertOperation(Info info,
         @RequestParam("a_logoPicPath")MultipartFile fileupload,
         HttpServletRequest request) throws IOException {

        String path = request.getSession().getServletContext().getRealPath("/statics/uploadfiles");

        info.setLogoPicPath(fileupload.getOriginalFilename());
        info.setCreationDate(new Date());
        if(!fileupload.isEmpty()){
            fileupload.transferTo(new File(path+File.separator+UUID.randomUUID()+fileupload.getOriginalFilename()));
        }
        devService.insertSelective(info);
        return "redirect:appinfolist";
    }




    /**
     * 总查询
     * @return
     */
    @RequestMapping(value = "appinfolist",method = RequestMethod.GET)
    public String appinfolistGet(UserExcesion userExcesion,String pageNum,Model model){
        Page<UserExcesion> list = devService.userInfo(userExcesion,pageNum);
        model.addAttribute("list",list);
        return "developer/appinfolist";
    }
    @RequestMapping(value = "appinfolist",method = RequestMethod.POST)
    public String appinfolistPost(
            @RequestParam(value = "querySoftwareName",required = false)String querySoftwareName,
            @RequestParam(value = "queryStatus",required = false)String queryStatus,
            @RequestParam(value = "queryFlatformId",required = false)String queryFlatformId,
            @RequestParam(value = "queryCategoryLevel1",required = false)String queryCategoryLevel1,
            @RequestParam(value = "queryCategoryLevel2",required = false)String queryCategoryLevel2,
            @RequestParam(value = "queryCategoryLevel3",required = false)String queryCategoryLevel3,
            @RequestParam(value = "pageIndex",required = false) String pageNum, Model model){

        model.addAttribute("querySoftwareName",querySoftwareName);
        model.addAttribute("queryStatus",queryStatus);
        model.addAttribute("queryFlatformId",queryFlatformId);
        model.addAttribute("queryCategoryLevel1",queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2",queryCategoryLevel2);
        model.addAttribute("queryCategoryLevel3",queryCategoryLevel3);


        UserExcesion userExcesion = new UserExcesion();
        userExcesion.setCategoryLevel1Name(queryCategoryLevel1);
        userExcesion.setCategoryLevel2Name(queryCategoryLevel2);
        userExcesion.setCategoryLevel3Name(queryCategoryLevel3);
        userExcesion.setFlatformIdName(queryFlatformId);
        userExcesion.setStatusName(queryStatus);
        userExcesion.setSoftwareName(querySoftwareName);

        Page<UserExcesion> list = devService.userInfo(userExcesion,pageNum);
        model.addAttribute("list",list);
        return "developer/appinfolist";
    }











    /**
     * 三级联动加载
     * @return
     */
    @RequestMapping("load1")
    public @ResponseBody Object load1(){
        List<Category> list=devService.grade();
        return JSON.toJSONString(list);
    }
    @RequestMapping("load2")
    public @ResponseBody Object load2(@RequestParam("pid")String id){
        return devService.grade2(id);
    }
    @RequestMapping("load3")
    public @ResponseBody Object load3(@RequestParam("pid")String id){
        return devService.grade3(id);
    }


    /**
     * 状态加载
     * @return
     */
    @RequestMapping("statusAjax")
    public @ResponseBody Object statusAjax(){
        return devService.statusList();
    }

    /**
     * 平台加载
     * @return
     */
    @RequestMapping("platAjax")
    public @ResponseBody Object platAjax(){
        return devService.flatfromList();
    }



    /**
     * apkName验证
     * @return
     */
    @RequestMapping("APKNameAjax")
    public @ResponseBody Object APKNameAjax(@RequestParam("APKName")String apkName)
    {
        System.out.println(apkName);
        String name = devService.ApkNameExist(apkName);
        System.out.println(name);
        if(name==null){
            return "name";
        }else {
            return "rename";
        }
    }


}